//
//  UIViewController+Tracking.m
//  YouShaQi
//
//  Created by iyuanxucheng on 2017/9/26.
//  Copyright © 2017年 HangZhou RuGuo Network Technology Co.Ltd. All rights reserved.
//

#import "UIViewController+Tracking.h"

@implementation UIViewController (Tracking)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        
        // ViewDidLoad
        SEL originalViewDidLoadSelector = @selector(viewDidLoad);
        SEL swizzledViewDidLoadSelector = @selector(tracking_viewDidLoad);
        
        Method originalViewDidLoadMethod = class_getInstanceMethod(class, originalViewDidLoadSelector);
        Method swizzledViewDidLoadMethod = class_getInstanceMethod(class, swizzledViewDidLoadSelector);
        
        BOOL didAddViewDidLoadMethod = class_addMethod
        (
         class,
         originalViewDidLoadSelector,
         method_getImplementation(swizzledViewDidLoadMethod),
         method_getTypeEncoding(swizzledViewDidLoadMethod)
         );
        
        if (didAddViewDidLoadMethod) {
            class_replaceMethod
            (
             class,
             swizzledViewDidLoadSelector,
             method_getImplementation(originalViewDidLoadMethod),
             method_getTypeEncoding(originalViewDidLoadMethod)
             );
        } else {
            method_exchangeImplementations(originalViewDidLoadMethod, swizzledViewDidLoadMethod);
        }
        
        // ViewWillAppear
        SEL originalViewWillAppearSelector = @selector(viewWillAppear:);
        SEL swizzledViewWillAppearSelector = @selector(tracking_viewWillAppear:);
        
        Method originalViewWillAppearMethod = class_getInstanceMethod(class, originalViewWillAppearSelector);
        Method swizzledViewWillAppearMethod = class_getInstanceMethod(class, swizzledViewWillAppearSelector);
        
        BOOL didAddViewWillAppearMethod = class_addMethod
        (
         class,
         originalViewWillAppearSelector,
         method_getImplementation(swizzledViewWillAppearMethod),
         method_getTypeEncoding(swizzledViewWillAppearMethod)
         );
        
        if (didAddViewWillAppearMethod) {
            class_replaceMethod
            (
             class,
             swizzledViewWillAppearSelector,
             method_getImplementation(originalViewWillAppearMethod),
             method_getTypeEncoding(originalViewWillAppearMethod)
             );
        } else {
            method_exchangeImplementations(originalViewWillAppearMethod, swizzledViewWillAppearMethod);
        }
        
        // ViewWillDisappear
        SEL originalViewWillDisappearSelector = @selector(viewWillDisappear:);
        SEL swizzledViewWillDisappearSelector = @selector(tracking_viewWillDisappear:);
        
        Method originalViewWillDisappearMethod = class_getInstanceMethod(class, originalViewWillDisappearSelector);
        Method swizzledViewWillDisappearMethod = class_getInstanceMethod(class, swizzledViewWillDisappearSelector);
        
        BOOL didAddViewWillDisappearMethod = class_addMethod
        (
         class,
         originalViewWillDisappearSelector,
         method_getImplementation(swizzledViewWillDisappearMethod),
         method_getTypeEncoding(swizzledViewWillDisappearMethod)
         );
        if (didAddViewWillDisappearMethod) {
            class_replaceMethod
            (
             class,
             swizzledViewWillDisappearSelector,
             method_getImplementation(originalViewWillDisappearMethod),
             method_getTypeEncoding(originalViewWillDisappearMethod)
             );
        } else {
            method_exchangeImplementations(originalViewWillDisappearMethod, swizzledViewWillDisappearMethod);
        }
        
        // viewDidAppear:
        SEL originalViewDidAppearSelector = @selector(viewDidAppear:);
        SEL swizzledViewDidAppearSelector = @selector(tracking_viewDidAppear:);
        
        Method originalViewDidAppearMethod = class_getInstanceMethod(class, originalViewDidAppearSelector);
        Method swizzledViewDidAppearMethod = class_getInstanceMethod(class, swizzledViewDidAppearSelector);
        
        BOOL didAddViewDidAppearMethod = class_addMethod
        (
         class,
         originalViewDidAppearSelector,
         method_getImplementation(originalViewDidAppearMethod),
         method_getTypeEncoding(swizzledViewDidAppearMethod)
         );
        if (didAddViewDidAppearMethod) {
            class_replaceMethod
            (
             class,
             swizzledViewDidAppearSelector,
             method_getImplementation(originalViewDidAppearMethod),
             method_getTypeEncoding(originalViewDidAppearMethod)
             );
        } else {
            method_exchangeImplementations(originalViewDidAppearMethod, swizzledViewDidAppearMethod);
        }
        
        // viewDidDisappear:
        SEL originalViewDidDisappearSelector = @selector(viewDidDisappear:);
        SEL swizzledViewDidDisAppearSelector = @selector(tracking_viewDidDisappear:);
        
        Method originalViewDidDisAppearMethod = class_getInstanceMethod(class, originalViewDidDisappearSelector);
        Method swizzledViewDidDisAppearMethod = class_getInstanceMethod(class, swizzledViewDidDisAppearSelector);
        
        BOOL didAddViewDidDisappearMethod = class_addMethod
        (
         class,
         originalViewDidDisappearSelector,
         method_getImplementation(originalViewDidDisAppearMethod),
         method_getTypeEncoding(swizzledViewDidDisAppearMethod)
         );
        if (didAddViewDidDisappearMethod) {
            class_replaceMethod
            (
             class,
             swizzledViewDidDisAppearSelector,
             method_getImplementation(originalViewDidDisAppearMethod),
             method_getTypeEncoding(originalViewDidDisAppearMethod)
             );
        } else {
            method_exchangeImplementations(originalViewDidDisAppearMethod, swizzledViewDidDisAppearMethod);
        }
    
        SEL originalDeallocSelector = NSSelectorFromString(@"dealloc");
        SEL swizzledDeallocSelector = @selector(tracking_dealloc);
        
        Method originalDeallocMethod = class_getInstanceMethod(class, originalDeallocSelector);
        Method swizzledDeallocMethod = class_getInstanceMethod(class, swizzledDeallocSelector);
        
        BOOL didAddDeallocMethod = class_addMethod
        (
         class,
         originalDeallocSelector,
         method_getImplementation(swizzledDeallocMethod),
         method_getTypeEncoding(swizzledDeallocMethod)
         );
        if (didAddDeallocMethod) {
            class_replaceMethod
            (
             class,
             swizzledDeallocSelector,
             method_getImplementation(swizzledDeallocMethod),
             method_getTypeEncoding(swizzledDeallocMethod)
             );
        } else {
            method_exchangeImplementations(originalDeallocMethod, swizzledDeallocMethod);
        }
    });
}

#pragma mark - Method Swizzling

- (void)tracking_viewWillAppear:(BOOL)animated {
    NSLog(@"view will appear : %@", NSStringFromClass([self class]));
    [self tracking_viewWillAppear:animated];
}

- (void)tracking_viewWillDisappear:(BOOL)animated {
    
    [self tracking_viewWillDisappear:animated];
}

- (void)tracking_viewDidAppear:(BOOL)animated {
    [self tracking_viewDidAppear:animated];
}


- (void)tracking_viewDidDisappear:(BOOL)animated {
    [self tracking_viewDidDisappear:animated];
}

- (void)tracking_viewDidLoad {
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:NULL];
    [self tracking_viewDidLoad];
}

- (void)tracking_dealloc {
    NSLog(@"view controller dealloc : %@", NSStringFromClass([self class]));
    [self tracking_dealloc];
}

@end
